import requests
import mysql.connector

import db


# 查询多条 UID 的 SQL 语句
sql_query = "SELECT user_id FROM user_register_info WHERE user_id > 5000000 AND user_id < 6000000 ORDER BY user_id DESC LIMIT 1000"  # 获取前1000个UID

# 从数据库中获取一批 UID
def get_uids_from_db():
    try:
        connection = db.getDbConnect()
        cursor = connection.cursor()
        cursor.execute(sql_query)
        result = cursor.fetchall()
        return [row[0] for row in result]  # 返回 UID 列表
    except mysql.connector.Error as err:
        print("数据库错误:", err)
        raise
    finally:
        if cursor:
            cursor.close()
        if connection:
            connection.close()

# API 请求部分
def send_post_request(uid):
    url = "https://staging-api.miyachat.com/services/phome/PHomeExtObj/FetchComprehensiveHomePage"

    headers = {
        "Content-Type": "application/json",
        "Authorization": "Bearer YOUR_ACCESS_TOKEN",  # 替换为实际Token
        "uid": str(uid)  # 从数据库中读取的UID
    }

    payload = {

    }


    try:
        response = requests.post(url, json=payload, headers=headers)
        if response.status_code == 200:
            resJson = response.json()
            print(f"UID {uid} 请求成功:", resJson)
            return "info" in resJson and "home_page_style" in resJson["info"] and resJson['info']['home_page_style'] == 1
        else:
            print(f"UID {uid} 请求失败，状态码: {response.status_code}, 响应: {response.text}")
    except requests.exceptions.RequestException as e:
        print(f"UID {uid} 请求异常:", e)
    return newTotal,oldTotal

# 主逻辑
if __name__ == "__main__":
    newTotal = 0
    oldTotal = 0
    try:
        uids = get_uids_from_db()  # 从数据库获取UID列表
        for uid in uids:  # 循环请求每个UID
            if send_post_request(uid) == True:
                newTotal = newTotal + 1
            else:
                oldTotal = oldTotal + 1
        print(f"新样式用户数: {newTotal}")
        print(f"旧样式用户数: {oldTotal}")
    except Exception as e:
        print("程序出错:", e)